package com.zc.angelica.config.sucurity;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zc.angelica.entity.*;
import com.zc.angelica.service.AccountClientService;
import com.zc.angelica.service.AccountPlatformInfoService;
import com.zc.angelica.service.AccountService;
import com.zc.angelica.service.RoleAccountInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserService implements UserDetailsService {

    @Autowired
    private AccountService accountService;

    @Autowired
    private RoleAccountInfoService roleAccountInfoService;

    @Autowired
    private AccountClientService accountClientService;

    @Autowired
    private AccountPlatformInfoService accountPlatformInfoService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<Account> accountWrapper = new QueryWrapper<>();
        accountWrapper.eq("username", username);
        Account account = accountService.getBaseMapper().selectOne(accountWrapper);
        if (account == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        QueryWrapper<RoleAccountInfo> roleAccountInfoQueryWrapper = new QueryWrapper<>();
        roleAccountInfoQueryWrapper.eq("account_id",account.pkVal());
        List<RoleAccountInfo> roleAccountInfos = roleAccountInfoService.getBaseMapper().selectList(roleAccountInfoQueryWrapper);
        if(roleAccountInfos.size() > 0){
            Collection<GrantedAuthority> authorities = new ArrayList<>();
            roleAccountInfos.forEach(roleInfo ->{
                authorities.add(new SimpleGrantedAuthority(roleInfo.getRoleCode()));
            });
            account.setAuthorities(authorities);
        }
        QueryWrapper<AccountClient> accountClientQueryWrapper = new QueryWrapper<>();
        accountClientQueryWrapper.eq("account_id",account.pkVal());
        List<AccountClient> accountClients = accountClientService.getBaseMapper().selectList(accountClientQueryWrapper);
        if (accountClients!=null&&accountClients.size() > 0){
                account.setLasting(accountClients);
        }
//        Object data = accountPlatformInfoService.selectAllPlatform().getData();
//        if (platformInfos!=null&&platformInfos.size() > 0){
//            List<PlatformInfo> platforms = platformInfos.stream().filter(item -> item.getBinding() != null && item.getBinding() == 2).collect(Collectors.toList());
//            if (platforms.size()>0){
//                account.setPlatforms(platforms);
//            }
//        }

        return account;
    }
}
